package com.ydzh.kfqgwvisitor.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * rabbitMQ的配置文件
 * <p>
 * 新商家审核通过 -> new_merchant_queue -> 死信消息交换机 ->死信队列
 */
@Configuration
public class RabbitMQConfig {

    /**
     * 声明死信交换机
     */
    public static final String LOCK_MERCHANT_DEAD_EXCHANGE = "lock_merchant_dead_exchange";

    /**
     * 声明死信队列
     */
    public static final String LOCK_MERCHANT_DEAD_QUEUE = "lock_merchant_dead_queue";

    /**
     * 声明进入死信队列的路由key
     */
    public static final String LOCK_MERCHANT_ROUTING_KEY = "lock_merchant_routing_key";

    /**
     * 创建死信交换机
     *
     * @return
     */
    @Bean
    public Exchange lockMerchantDeadExchange() {
        return new TopicExchange(LOCK_MERCHANT_DEAD_EXCHANGE, true, false);
    }

    /**
     * 创建死信队列
     *
     * @return
     */
    @Bean
    public Queue lockMerchantDeadQueue() {
        return QueueBuilder.durable(LOCK_MERCHANT_DEAD_QUEUE).build();
    }

    /**
     * 绑定死信交换机和死信队列
     *
     * @return
     */
    @Bean
    public Binding lockMerchantBinding() {

        return new Binding(LOCK_MERCHANT_DEAD_QUEUE, Binding.DestinationType.QUEUE,
                LOCK_MERCHANT_DEAD_EXCHANGE, LOCK_MERCHANT_ROUTING_KEY, null);

    }

    /**
     * 声明普通队列，绑定死信交换机
     */
    public static final String NEW_MERCHANT_QUEUE = "new_merchant_queue";

    /**
     * 声明普通的topic交换机
     */
    public static final String NEW_MERCHANT_EXCHANGE = "new_merchant_exchange";

    /**
     * 声明路由key
     */
    public static final String NEW_MERCHANT_ROUTING_KEY = "new_merchant_routing_key";

    /**
     * 创建普通交换机
     *
     * @return
     */
    @Bean
    public Exchange newMerchantExchange() {
        return new TopicExchange(NEW_MERCHANT_EXCHANGE, true, false);
    }

    /**
     * 创建普通队列
     *
     * @return
     */
    @Bean
    public Queue newMerchantQueue() {

        Map<String, Object> map = new HashMap<>(3);

        //消息过期后，进入到死信交换机
        map.put("x-dead-letter-exchange", LOCK_MERCHANT_DEAD_EXCHANGE);

        //消息过期后，进入到死信交换机的路由key
        map.put("x-dead-letter-routing-key", LOCK_MERCHANT_ROUTING_KEY);

        //过期时间，单位毫秒
        map.put("x-message-ttl", 10000);

        return QueueBuilder.durable(NEW_MERCHANT_QUEUE).withArguments(map).build();
    }

    /**
     * 绑定普通交换机和普通队列
     *
     * @return
     */
    @Bean
    public Binding newMerchantBinding() {

        return new Binding(NEW_MERCHANT_QUEUE, Binding.DestinationType.QUEUE,
                NEW_MERCHANT_EXCHANGE, NEW_MERCHANT_ROUTING_KEY, null);

    }


//    /**
//     * 交换机名称
//     */
//    public static final String EXCHANGE_NAME = "order_exchange";
//
//    /**
//     * 队列名
//     */
//    public static final String QUEUE = "order_queue";
//
//    /**
//     * 声明(创建)topic交换机
//     *
//     * @return
//     */
//    @Bean
//    public Exchange orderExchange() {
//        //durable()的意思是是否持久化
//        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
//    }
//
//    /**
//     * 声明(创建)队列
//     *
//     * @return
//     */
//    @Bean
//    public Queue orderQueue() {
//        return QueueBuilder.durable(QUEUE).build();
//    }
//
//    /**
//     * 交换机和队列进行绑定
//     *
//     * @param queue
//     * @param exchange
//     * @return
//     */
//    @Bean
//    public Binding orderBingding(Queue queue, Exchange exchange) {
//        return BindingBuilder.bind(queue).to(exchange).with("order.#").noargs();
//    }
}
